﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace FromAccToSql
{
    class PDT
    {
        public static void InsertModuleType(SqlConnection cnn, object moduleType_Id, object name, object description)
        {
            string cmdText = "INSERT INTO ModuleType ([ModuleType_Id],[Name],[Description])" +
            "VALUES (@ModuleType_Id, @Name, @Description )";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("ModuleType_Id", moduleType_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Name", name));
            sqlcmd.Parameters.Add(new SqlParameter("Description", description));

            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void InsertModuleParameter(SqlConnection cnn, object module_Id, object name, object nr, object param_Value, object param_String, object unit)
        {
            string cmdText = "INSERT INTO ModuleParameter ([Module_Id],[Name],[Nr],[Value],[StringValue],[Unit])" +
            "VALUES (@Module_Id, @Name, @Nr, @Value, @StringValue, @Unit )";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("Module_Id", module_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Name", name));
            sqlcmd.Parameters.Add(new SqlParameter("Nr", nr));
            sqlcmd.Parameters.Add(new SqlParameter("Value", param_Value));
            sqlcmd.Parameters.Add(new SqlParameter("StringValue", param_String));
            sqlcmd.Parameters.Add(new SqlParameter("Unit", unit));

            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void InsertModuleTypeParameter(SqlConnection cnn, object moduleType_Id, object name, object nr, object param_Value, object param_String, object unit)
        {
            string cmdText = "INSERT INTO ModuleTypeParameter ([ModuleType_Id],[Name],[Nr],[Value],[StringValue],[Unit])" +
            "VALUES (@ModuleType_Id, @Name, @Nr, @Value, @StringValue, @Unit )";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("ModuleType_Id", moduleType_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Name", name));
            sqlcmd.Parameters.Add(new SqlParameter("Nr", nr));
            sqlcmd.Parameters.Add(new SqlParameter("Value", param_Value));
            sqlcmd.Parameters.Add(new SqlParameter("StringValue", param_String));
            sqlcmd.Parameters.Add(new SqlParameter("Unit", unit));

            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void InsertModule(SqlConnection cnn, object module_Id, object moduleType_Id, object name, object description, object used, object config)
        {
            string cmdText = "INSERT INTO Module ([Module_Id],[ModuleType_Id],[Name],[Description],[Used],[Config])" +
            "VALUES (@Module_Id, @ModuleType_Id, @Name, @Description, @Used, @Config )";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("Module_Id", module_Id));
            sqlcmd.Parameters.Add(new SqlParameter("ModuleType_Id", moduleType_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Name", name));
            sqlcmd.Parameters.Add(new SqlParameter("Description", description));
            sqlcmd.Parameters.Add(new SqlParameter("Used", used));
            sqlcmd.Parameters.Add(new SqlParameter("Config", config));

            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void InsertChannelType(SqlConnection cnn, object channelType_Id, object baseType, object description, object unit)
        {
            string cmdText = "INSERT INTO ChannelType ([ChannelType_Id],[BaseType],[Description],[Unit])" +
            "VALUES (@ChannelType_Id, @BaseType, @Description, @Unit )";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("ChannelType_Id", channelType_Id));
            sqlcmd.Parameters.Add(new SqlParameter("BaseType", baseType));
            sqlcmd.Parameters.Add(new SqlParameter("Description", description));
            sqlcmd.Parameters.Add(new SqlParameter("Unit", unit));

            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void InsertChannelAddress(SqlConnection cnn, object channelAddress_Id, object port, object drive, object unit, object subunit,
                  object AddressByte, object AddressBit, object inverted, object hi, object low, object wordLength, object on_Error, object asyncType)
        {
            string cmdText = "INSERT INTO ChannelAddress ([ChannelAddress_Id], [Port],[Drive],[Unit],[Subunit],[AddressByte],[AddressBit],[Inverted],[Hi],[Low],[WordLength],[On_Error],[AsyncType])" +
                " VALUES (@ChannelAddress_Id, @Port, @Drive, @Unit, @Subunit, @AddressByte, @AddressBit, @Inverted, @Hi, @Low, @WordLength, @On_Error, @AsyncType)";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("ChannelAddress_Id", channelAddress_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Port", port));
            sqlcmd.Parameters.Add(new SqlParameter("Drive", drive));
            sqlcmd.Parameters.Add(new SqlParameter("Unit", unit));
            sqlcmd.Parameters.Add(new SqlParameter("Subunit", subunit));
            sqlcmd.Parameters.Add(new SqlParameter("AddressByte", AddressByte));
            sqlcmd.Parameters.Add(new SqlParameter("AddressBit", AddressBit));
            sqlcmd.Parameters.Add(new SqlParameter("Inverted", inverted));
            sqlcmd.Parameters.Add(new SqlParameter("Hi", hi));
            sqlcmd.Parameters.Add(new SqlParameter("Low", low));
            sqlcmd.Parameters.Add(new SqlParameter("WordLength", wordLength));
            sqlcmd.Parameters.Add(new SqlParameter("On_Error", on_Error));
            sqlcmd.Parameters.Add(new SqlParameter("AsyncType", asyncType));

            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }
              
        public static void InsertChannel(SqlConnection cnn, object channel_Id, object channelAddress_Id, object channelType_Id, object name, object description,
                  object used, object interval, object ordNr, object transtype, object factor, object offset, object operation, object saveStat, object sign, object interval1)
        {
            string cmdText = "INSERT INTO Channel ([Channel_Id], [ChannelAddress_Id], [ChannelType_Id], [Name], [Description], [Used], [Interval], [OrdNr], [Transtype]," +
                " [Factor], [Offset], [Operation], [SaveStat], [Sign], [Interval1])" +
                " VALUES (@Channel_Id, @ChannelAddress_Id, @ChannelType_Id, @Name, @Description, @Used, @Interval, @OrdNr, @Transtype," +
                "@Factor, @Offset, @Operation, @SaveStat, @Sign, @Interval1)";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("Channel_Id", channel_Id));
            sqlcmd.Parameters.Add(new SqlParameter("ChannelAddress_Id", channelAddress_Id));
            sqlcmd.Parameters.Add(new SqlParameter("ChannelType_Id", channelType_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Name", name));
            sqlcmd.Parameters.Add(new SqlParameter("Description", description ));
            sqlcmd.Parameters.Add(new SqlParameter("Used", used));
            sqlcmd.Parameters.Add(new SqlParameter("Interval", interval));
            sqlcmd.Parameters.Add(new SqlParameter("OrdNr", ordNr));
            sqlcmd.Parameters.Add(new SqlParameter("Transtype", transtype));
            sqlcmd.Parameters.Add(new SqlParameter("Factor", factor));
            sqlcmd.Parameters.Add(new SqlParameter("Offset", offset));
            sqlcmd.Parameters.Add(new SqlParameter("Operation", operation));
            sqlcmd.Parameters.Add(new SqlParameter("SaveStat", saveStat));
            sqlcmd.Parameters.Add(new SqlParameter("Sign", sign));
            sqlcmd.Parameters.Add(new SqlParameter("Interval1", interval1));

            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void InsertBase_Ch(SqlConnection cnn, object channel, object base_ch, object neg)
        {
            string cmdText = "INSERT INTO Base_Ch ([Channel], [Base_Ch], [Neg]) VALUES (@Channel, @Base_Ch, @Neg)";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("Channel", channel));
            sqlcmd.Parameters.Add(new SqlParameter("Base_Ch", base_ch));
            sqlcmd.Parameters.Add(new SqlParameter("Neg", neg));
          
            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void InsertModuleType_ChannelType(SqlConnection cnn, object moduleType_Id, object channelType_Id, object nr, object name)
        {
            string cmdText = "INSERT INTO ModuleType_ChannelType([ModuleType_Id], [ChannelType_Id], [Nr], [Name]) VALUES (@ModuleType_Id, @ChannelType_Id, @Nr, @Name)";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("ModuleType_Id", moduleType_Id));
            sqlcmd.Parameters.Add(new SqlParameter("ChannelType_Id", channelType_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Nr", nr));
            sqlcmd.Parameters.Add(new SqlParameter("Name", name));

            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void InsertModule_Channel(SqlConnection cnn, object module_Id, object channel_Id, object nr)
        {
            string cmdText = "INSERT INTO Module_Channel([Module_Id], [Channel_Id], [Nr]) VALUES (@Module_Id, @Channel_Id, @Nr)";

            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = cmdText;

            sqlcmd.Parameters.Add(new SqlParameter("Module_Id", module_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Channel_Id", channel_Id));
            sqlcmd.Parameters.Add(new SqlParameter("Nr", nr));


            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        public static void SetIdentityInsertOnOff(SqlConnection cnn, string tableName, bool isOn)
        {
            var sqlcmd = new SqlCommand();
            sqlcmd.Connection = cnn;
            sqlcmd.CommandType = System.Data.CommandType.Text;

            sqlcmd.CommandText = string.Format("set identity_insert {0} {1}", tableName, isOn ? "On" : "Off");
            sqlcmd.ExecuteNonQuery();

        }
    }
}
